package com.cxs.ormdemo.slice;

import com.cxs.ormdemo.ResourceTable;
import com.cxs.ormdemo.db.BookStore;
import com.cxs.ormdemo.db.model.Author;
import com.cxs.ormdemo.db.model.Book;
import com.cxs.ormdemo.db.model.User;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.data.DatabaseHelper;
import ohos.data.orm.*;

import java.util.List;

public class MainAbilitySlice extends AbilitySlice {

    OrmContext bookStore;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initDB();
        initView();

    }

    private void initDB() {
        DatabaseHelper helper = new DatabaseHelper(this);
        bookStore = helper.getOrmContext("BookStore", "bookstore.db", BookStore.class);
//        bookStore.registerEntityObserver("user", new OrmObjectObserver() {
//            @Override
//            public void onChange(OrmContext ormContext, AllChangeToTarget allChangeToTarget) {
//                System.out.println(ormContext.getAlias());
//                System.out.println(allChangeToTarget.getInsertedList());
//                System.out.println(allChangeToTarget.getUpdatedList());
//                System.out.println(allChangeToTarget.getDeletedList());
//            }
//        });

        bookStore.registerContextObserver(bookStore, new OrmObjectObserver() {
            @Override
            public void onChange(OrmContext ormContext, AllChangeToTarget allChangeToTarget) {

                System.out.println(allChangeToTarget.getInsertedList());
                System.out.println(allChangeToTarget.getUpdatedList());
                System.out.println(allChangeToTarget.getDeletedList());
            }
        });
    }

    private void initView() {
        findComponentById(ResourceTable.Id_btn_query)
                .setClickedListener(c -> queryDB());
        findComponentById(ResourceTable.Id_btn_insert)
                .setClickedListener(c -> insertDB());
        findComponentById(ResourceTable.Id_btn_update)
                .setClickedListener(c -> updateDB());
        findComponentById(ResourceTable.Id_btn_delete)
                .setClickedListener(c -> deleteDB());
        findComponentById(ResourceTable.Id_btn_backup)
                .setClickedListener(c -> backupDB());
        findComponentById(ResourceTable.Id_btn_restore)
                .setClickedListener(c -> restoreDB());
        findComponentById(ResourceTable.Id_btn_transaction_insert)
                .setClickedListener(c -> transactionInsertDB());
    }

    private void restoreDB() {
        bookStore.restore("bookstore_backup.db");
    }

    private void backupDB() {
        bookStore.backup("bookstore_backup.db");

    }

    private void transactionInsertDB() {
        bookStore.beginTransaction();
        try {
            for (int i = 0; i < 5; i++) {
                User user = new User(20, "王五");
                bookStore.insert(user);
            }
            bookStore.flush();
            bookStore.commit();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            bookStore.rollback();
        }
    }

    private void deleteDB() {
        OrmPredicates predicates = bookStore.where(User.class).equalTo("age", 18);
        List<User> users = bookStore.query(predicates);
        for (User user : users) {
            bookStore.delete(user);
        }
        bookStore.flush();
//        OrmPredicates predicates = bookStore.where(User.class).equalTo("age", 18);
//        int num = bookStore.delete(predicates);
//        System.out.println("=================删除 " + num);
    }

    private void updateDB() {
        OrmPredicates predicates = bookStore.where(User.class).equalTo("age", 18);
        List<User> users = bookStore.query(predicates);
        for (User user : users) {
            user.setName("张三");
            bookStore.update(user);
        }
        bookStore.flush();
    }

    private void queryDB() {
        OrmPredicates predicates = bookStore.where(User.class);
        List<User> users = bookStore.query(predicates);
        for (User user : users) {
            System.out.println("================user================");
            System.out.println(user);
            System.out.println("================user================");
        }
    }

    private void insertDB() {
        User user = new User(18, "zhangsan");
        boolean inserted = bookStore.insert(user);
        boolean flush = bookStore.flush();
        System.out.println("================inserted " + inserted);
        System.out.println("================flush " + flush);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onStop() {
        super.onStop();
        bookStore.close();
    }
}
